package com.lzy.cloud.goods.controller;

import com.github.pagehelper.PageInfo;
import com.lzy.cloud.goods.dtos.CarSeriesDto;
import com.lzy.cloud.goods.facade.CarSeriesFacade;
import com.lzy.cloud.goods.request.CarSeriesRequest;
import com.lzy.cloud.goods.response.CarSeriesResponse;
import com.lzy.cloud.goods.service.CarSeriesService;
import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.orm.db.Pager;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * Created by WIN7 on 2018/6/11.
 */
@RestController
@RequestMapping("/goods/car/series")
public class CarSeriesController {

    @Autowired
    CarSeriesService carSeriesService;

    @Autowired
    CarSeriesFacade carSeriesFacade;

    /**
     * 新增车型系列
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/add" ,method = RequestMethod.POST)
    public Result add(@RequestBody(required = false) CarSeriesRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = carSeriesService.add(DozerUtils.map(request, CarSeriesDto.class));
        Ensure.that(ok).isTrue("00050001");
        return new Result(ok);
    }

    /**
     * 删除车型系列
     *
     * @param ids
     * @return
     */
    @RequestMapping(value = "/del/{ids}" ,method = RequestMethod.GET)
    public Result del(@PathVariable("ids") String ids){
        Boolean ok = carSeriesFacade.del(ids);
        Ensure.that(ok).isTrue("00050002");
        return new Result(ok);
    }

    /**
     * 编辑车型系列
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/update",method = RequestMethod.PUT)
    public Result update(@RequestBody CarSeriesRequest request){
        Ensure.that(request).isNotNull("00010001");
        Boolean ok = carSeriesService.update(DozerUtils.map(request,CarSeriesDto.class));
        Ensure.that(ok).isTrue("00050003");
        return new Result(ok);
    }

    /**
     * 根据id查询车型系列
     *
     * @param id
     * @return
     */
    @RequestMapping(value = "/detail/{id}" ,method = RequestMethod.GET)
    public Result detail(@PathVariable("id") Long id){
        CarSeriesDto carSeriesDto = carSeriesService.selectById(id);
        Ensure.that(carSeriesDto).isNotNull("00050004");
        return new Result(DozerUtils.map(carSeriesDto,CarSeriesResponse.class));
    }

    /**
     * 分页查询列表
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public Result selectList(CarSeriesRequest request){
        Ensure.that(request).isNotNull("00010001");
        Pager pager = new Pager();
        pager.setPageNum(request.getPageNum());
        pager.setPageSize(request.getPageSize());
        PageInfo<CarSeriesDto> pageInfo = carSeriesService.selectList(pager,DozerUtils.map(request,CarSeriesDto.class));
        if (pageInfo != null && pageInfo.getList() != null) {
            pager.setTotal(pageInfo.getTotal());
            return new Result(pager,DozerUtils.maps(pageInfo.getList(), CarSeriesResponse.class));
        } else {
            return new Result();
        }
    }
}
